package com.oocl.o2o.merchant.dao.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

import org.springframework.stereotype.Repository;

import com.oocl.o2o.merchant.dao.OrderInfoDao;
import com.oocl.o2o.merchant.entity.OrderInfo;

@Repository
public class OrderInfoDaoImpl implements OrderInfoDao {

	@PersistenceContext
	private EntityManager em;

	@Override
	public OrderInfo add(OrderInfo t) {
		try {
			em.persist(t);
			return t;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public OrderInfo update(OrderInfo t) {
		try {
			t = em.merge(t);
			return t;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public OrderInfo delete(String id) {
		try {
			OrderInfo orderInfo = em.find(OrderInfo.class, id);
			em.remove(orderInfo);
			return orderInfo;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public OrderInfo findById(String id) {
		try {
			return em.find(OrderInfo.class, id);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public List<OrderInfo> findAll() {
		try {
			String jpql = "select oi from OrderInfo oi";

			TypedQuery<OrderInfo> query = em.createQuery(jpql, OrderInfo.class);
			return query.getResultList();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

}
